﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tunnel.DevicesControls;
using Tunnel.DevicesControls.Lamp;
using Tunnel.DevicesControls.TempFiber;

namespace Tunnel
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 定义数据传输服务实例
        /// </summary>
        DataConnectionService dcs;
        /// <summary>
        /// 定义服务承载
        /// </summary>
        ServiceHost dcsHost;

        DataTable allStatusTable;
        private void MainForm_Load(object sender, EventArgs e)
        {
            mainMenu.Visible = false;
            //mainMenu.Parent = mainRoadPic;
            mainMenu.Location = new Point(0, Convert.ToInt32(this.Height * 0.15));
            menuEntry.Location = new Point(0, Convert.ToInt32(this.Height * 0.15));
            titlePic.Location = new Point(0, 0);
            titlePic.Size = new Size(this.Width, Convert.ToInt32(this.Height * 0.15));

            dcs = new DataConnectionService(this);
            dcsHost = new ServiceHost(dcs);
            Tools.FanTimeControl.init();
            try
            {
                dcsHost.Open();
            }
            catch (System.ServiceModel.AddressAlreadyInUseException ex)
            {
                MessageBox.Show("当前数据接口服务地址已被占用，请检查是否开启多个监控程序，详细错误如下：\r\n" + ex.Message);
                Application.Exit();
            }
            catch (Exception ex)
            {
                MessageBox.Show("数据接口服务开启失败，程序将退出，错误编号：\r\n" + ex.Message);
                Application.Exit();
            }
            deplyDevices();
            //显示第一组设备
            ShowDeviceGroup1();

            mainRoadPic.Location = new Point(0, 0);
            appExitPic.Parent = titlePic;
            appExitPic.Location = new Point(this.Size.Width - appExitPic.Size.Width, 0);

            mainTimer.Interval = Properties.Settings.Default.RefreshTime * 1000;
            mainTimer.Start();
            refreshBackground.RunWorkerAsync();


        }
        #region 定义各类设备集合
        List<Fan> FANs = new List<Fan>();
        List<COVI> COVIs = new List<COVI>();
        List<CCTV_Round> CCTV_Rounds = new List<CCTV_Round>();
        List<CCTV_CloudColor> CCTV_CloudColors = new List<CCTV_CloudColor>();
        List<CCTV_FixedColor> CCTV_FixedColors = new List<CCTV_FixedColor>();
        List<TS> TSs = new List<TS>();
        List<FD> FDs = new List<FD>();
        List<FCMS> FCMSs = new List<FCMS>();
        List<ET> ETs = new List<ET>();
        List<Light_BasicLED10> Light_BasicLED10s = new List<Light_BasicLED10>();
        List<Light_BasicLED21> Light_BasicLED21s = new List<Light_BasicLED21>();
        List<Light_BasicLED22> Light_BasicLED22s = new List<Light_BasicLED22>();
        List<Light_CloudS1> Light_CloudS1s = new List<Light_CloudS1>();
        List<Light_CloudS2> Light_CloudS2s = new List<Light_CloudS2>();
        List<Light_EmergencyLED> Light_EmergencyLEDs = new List<Light_EmergencyLED>();
        List<Light_Out> Light_Outs = new List<Light_Out>();
        List<Light_Parking> Light_Parkings = new List<Light_Parking>();
        List<Light_SunnyS1> Light_SunnyS1s = new List<Light_SunnyS1>();
        List<Light_SunnyS2> Light_SunnyS2s = new List<Light_SunnyS2>();
        List<FiberPoint> Fibers = new List<FiberPoint>();
        List<SOMKE> Somkes = new List<SOMKE>();
        List<LC> LCs = new List<LC>();
        #endregion

        /// <summary>
        /// 更改设备状态接口实现
        /// </summary>
        /// <param name="deviceID">设备ID</param>
        /// <param name="statusID">设备状态ID</param>
        /// <param name="statusDescription">设备状态描述</param>
        /// <param name="deviceType">设备类型</param>
        /// <param name="updateTime">更新时间</param>
        /// <returns></returns>
        public bool refreshDeviceStatus(string deviceID, string statusID, string statusDescription, string deviceType, string updateTime)
        {
            string a = deviceType;
            if (deviceType == "FAN")
            {
                Fan s = FANs.Find(delegate (Fan d) { return d.DeviceID == deviceID; });
                s.StatusID = statusID;
                s.StatusDescription = statusDescription;
                s.UpdateTime = Convert.ToDateTime(updateTime);
                if (statusDescription == "正转" || statusDescription == "反转")
                {
                    Tools.FanTimeControl.LastFanTime = DateTime.Now;
                }
                return true;
            }
            if (deviceType == "COVI")
            {
                COVI s = COVIs.Find(delegate (COVI d) { return d.DeviceID == deviceID; });
                s.StatusID = statusID;
                s.StatusDescription = statusDescription;
                s.UpdateTime = Convert.ToDateTime(updateTime);
                return true;
            }
            if (deviceType == "CCTV_Round")
            {
                CCTV_Round s = CCTV_Rounds.Find(delegate (CCTV_Round d) { return d.DeviceID == deviceID; });
                s.StatusID = statusID;
                s.StatusDescription = statusDescription;
                s.UpdateTime = Convert.ToDateTime(updateTime);
                return true;
            }
            if (deviceType == "CCTV_CloudColor")
            {
                CCTV_CloudColor s = CCTV_CloudColors.Find(delegate (CCTV_CloudColor d) { return d.DeviceID == deviceID; });
                s.StatusID = statusID;
                s.StatusDescription = statusDescription;
                s.UpdateTime = Convert.ToDateTime(updateTime);
                return true;
            }
            if (deviceType == "CCTV_FixedColor")
            {
                CCTV_FixedColor s = CCTV_FixedColors.Find(delegate (CCTV_FixedColor d) { return d.DeviceID == deviceID; });
                s.StatusID = statusID;
                s.StatusDescription = statusDescription;
                s.UpdateTime = Convert.ToDateTime(updateTime);
                return true;
            }
            if (deviceType == "TS")
            {
                TS s = TSs.Find(delegate (TS d) { return d.DeviceID == deviceID; });
                s.StatusID = statusID;
                s.StatusDescription = statusDescription;
                s.UpdateTime = Convert.ToDateTime(updateTime);
                return true;
            }
            if (deviceType == "FD")
            {
                FD s = FDs.Find(delegate (FD d) { return d.DeviceID == deviceID; });
                s.StatusID = statusID;
                s.StatusDescription = statusDescription;
                s.UpdateTime = Convert.ToDateTime(updateTime);
                return true;
            }
            //if (deviceType == "FCMS")
            //{
            //    FCMS s = FCMSs.Find(delegate (FCMS d) { return d.DeviceID == deviceID; });
            //    s.StatusID = statusID;
            //    s.StatusDescription = statusDescription;
            //    s.UpdateTime = Convert.ToDateTime(updateTime);
            //    return true;
            //}
            if (deviceType == "ET")
            {
                ET s = ETs.Find(delegate (ET d) { return d.DeviceID == deviceID; });
                s.StatusID = statusID;
                s.StatusDescription = statusDescription;
                s.UpdateTime = Convert.ToDateTime(updateTime);
                return true;
            }
            if (deviceType == "Light_BasicLED_50W10")
            {
                foreach (Light_BasicLED10 s in Light_BasicLED10s)
                {
                    s.StatusID = statusID;
                    s.StatusDescription = statusDescription;
                    s.UpdateTime = Convert.ToDateTime(updateTime);
                }
                return true;
            }
            if (deviceType == "Light_BasicLED_50W21")
            {
                foreach (Light_BasicLED21 s in Light_BasicLED21s)
                {
                    s.StatusID = statusID;
                    s.StatusDescription = statusDescription;
                    s.UpdateTime = Convert.ToDateTime(updateTime);
                }
                return true;
            }
            if (deviceType == "Light_BasicLED_50W22")
            {
                foreach (Light_BasicLED22 s in Light_BasicLED22s)
                {
                    s.StatusID = statusID;
                    s.StatusDescription = statusDescription;
                    s.UpdateTime = Convert.ToDateTime(updateTime);
                }
                return true;
            }
            if (deviceType == "Light_CloudS_150W1")
            {
                foreach (Light_CloudS1 s in Light_CloudS1s)
                {
                    s.StatusID = statusID;
                    s.StatusDescription = statusDescription;
                    s.UpdateTime = Convert.ToDateTime(updateTime);
                }
                return true;
            }
            if (deviceType == "Light_CloudS_150W2")
            {
                foreach (Light_CloudS2 s in Light_CloudS2s)
                {
                    s.StatusID = statusID;
                    s.StatusDescription = statusDescription;
                    s.UpdateTime = Convert.ToDateTime(updateTime);
                }
                return true;
            }
            if (deviceType == "Light_EmergencyLED_50W")
            {
                foreach (Light_EmergencyLED s in Light_EmergencyLEDs)
                {
                    s.StatusID = statusID;
                    s.StatusDescription = statusDescription;
                    s.UpdateTime = Convert.ToDateTime(updateTime);
                }
                return true;
            }
            if (deviceType == "Light_Out_90W")
            {
                foreach (Light_Out s in Light_Outs)
                {
                    s.StatusID = statusID;
                    s.StatusDescription = statusDescription;
                    s.UpdateTime = Convert.ToDateTime(updateTime);
                }
                return true;
            }

            if (deviceType == "Light_SunnyS_150W1")
            {
                foreach (Light_SunnyS1 s in Light_SunnyS1s)
                {
                    s.StatusID = statusID;
                    s.StatusDescription = statusDescription;
                    s.UpdateTime = Convert.ToDateTime(updateTime);
                }
                return true;
            }
            if (deviceType == "Light_SunnyS_150W2")
            {
                foreach (Light_SunnyS2 s in Light_SunnyS2s)
                {
                    s.StatusID = statusID;
                    s.StatusDescription = statusDescription;
                    s.UpdateTime = Convert.ToDateTime(updateTime);
                }
                return true;
            }
            if (deviceType == "Fiber")
            {
                FiberPoint s = Fibers.Find(delegate (FiberPoint d) { return d.DeviceID == deviceID; });
                s.StatusID = statusID;
                s.StatusDescription = statusDescription;
                s.UpdateTime = Convert.ToDateTime(updateTime);
                return true;
            }
            if (deviceType == "Somke")
            {
                SOMKE s = Somkes.Find(delegate (SOMKE d) { return d.DeviceID == deviceID; });
                s.StandardID = statusID;
                s.StatusDescription = statusDescription;
                s.UpdateTime = Convert.ToDateTime(updateTime);
                return true;
            }
            if (deviceType == "FCMS")
            {
                FCMS s = FCMSs.Find(delegate (FCMS d) { return d.DeviceID == deviceID; });
                s.StandardID = statusID;
                s.StatusDescription = statusDescription;
                s.UpdateTime = Convert.ToDateTime(updateTime);
                MessageBox.Show("情报板发送成功", "信息发布成功", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                return true;
            }
            if (deviceType == "LC")
            {
                LC s = LCs.Find(delegate (LC d) { return d.DeviceID == deviceID; });
                s.StatusID = statusID;
                s.StatusDescription = statusDescription;
                s.UpdateTime = Convert.ToDateTime(updateTime);

                if (statusDescription == "正常")
                {
                    Global.PlcStatus[deviceID] = true;
                }
                if (statusDescription == "故障")
                {
                    Global.PlcStatus[deviceID] = false;
                }
                return true;
            }

            return false;
        }
        /// <summary>
        /// 修改设备数值
        /// </summary>
        /// <param name="deviceID">设备ID</param>
        /// <param name="numType">数值类型</param>
        /// <param name="num1">数值1</param>
        /// <param name="num2">数值2</param>
        /// <param name="num3">数值3</param>
        /// <returns></returns>
        public bool refreshDeviceNum(string deviceID, string numType, int num1, int num2, int num3)
        {
            if (numType == "CO")
            {
                COVI s = COVIs.Find(delegate (COVI d) { return d.DeviceID == deviceID; });
                s.Co = num1;

                return true;
            }
            if (numType == "VI")
            {
                COVI s = COVIs.Find(delegate (COVI d) { return d.DeviceID == deviceID; });
                s.Vi = num1;

                return true;
            }
            return false;
        }

        /// <summary>
        /// 部署设备
        /// </summary>
        public void deplyDevices()
        {
            #region 展示所有设备
            FANs.Clear();
            COVIs.Clear();
            CCTV_Rounds.Clear();
            CCTV_CloudColors.Clear();
            CCTV_FixedColors.Clear();
            TSs.Clear();
            FDs.Clear();
            FCMSs.Clear();
            ETs.Clear();
            Light_BasicLED10s.Clear();
            Light_BasicLED21s.Clear();
            Light_BasicLED22s.Clear();
            Light_CloudS1s.Clear();
            Light_CloudS2s.Clear();
            Light_EmergencyLEDs.Clear();
            Light_Outs.Clear();
            Light_Parkings.Clear();
            Light_SunnyS1s.Clear();
            Light_SunnyS2s.Clear();
            Somkes.Clear();
            LCs.Clear();

            foreach (Control c in this.mainRoadPic.Controls)
            {
                c.Dispose();
            }

            string allDevicesSqlString = "select * from M_Device,M_DeviceType where M_Device.TypeID=M_DeviceType.TypeID ";
            SqlDBHelper sdh = new SqlDBHelper();
            DataTable dtAllDevices = sdh.GetTable(allDevicesSqlString);

            var dtfans = from s1 in dtAllDevices.AsEnumerable().AsParallel()
                         where s1.Field<string>("TypeID") == "FAN"
                         select s1;

            //Parallel.ForEach<DataRow>(dtfans, item =>
            //{
            //    Fan fan = new Fan();
            //    mainRoadPic.Invoke(new Action(() =>
            //    {
            //        fan.Parent = mainRoadPic;
            //        fan.DeviceID = item["DeviceID"].ToString();
            //        fan.Name = item["Name"].ToString();
            //        fan.TypeID = item["TypeID"].ToString();
            //        fan.DevicePos = item["DevicePos"].ToString();
            //        fan.PicX = Convert.ToInt32(item["PicX"].ToString());
            //        fan.PicY = Convert.ToInt32(item["PicY"].ToString());
            //        fan.Location = new Point(fan.PicX, fan.PicY);
            //        if (!string.IsNullOrEmpty(item["Protocol"].ToString()))
            //        {
            //            fan.Protocol = item["Protocol"].ToString();
            //        }
            //        FANs.Add(fan);
            //    }));


            //});

            foreach (var item in dtfans)
            {
                Fan fan = new Fan();
                fan.Parent = mainRoadPic;
                fan.DeviceID = item["DeviceID"].ToString();
                fan.Name = item["Name"].ToString();
                fan.TypeID = item["TypeID"].ToString();
                fan.DevicePos = item["DevicePos"].ToString();
                fan.PicX = Convert.ToInt32(item["PicX"].ToString());
                fan.PicY = Convert.ToInt32(item["PicY"].ToString());
                fan.Location = new Point(fan.PicX, fan.PicY);
                if (!string.IsNullOrEmpty(item["Protocol"].ToString()))
                {
                    fan.Protocol = item["Protocol"].ToString();
                }
                FANs.Add(fan);
            }

            var dtcovi = from s2 in dtAllDevices.AsEnumerable()
                         where s2.Field<string>("TypeID") == "COVI"
                         select s2;
            foreach (var item in dtcovi)
            {
                COVI covi = new COVI();
                covi.Parent = mainRoadPic;
                covi.DeviceID = item["DeviceID"].ToString();
                covi.Name = item["Name"].ToString();
                covi.TypeID = item["TypeID"].ToString();
                covi.DevicePos = item["DevicePos"].ToString();
                covi.PicX = Convert.ToInt32(item["PicX"].ToString());
                covi.PicY = Convert.ToInt32(item["PicY"].ToString());
                covi.Location = new Point(covi.PicX, covi.PicY);
                if (!string.IsNullOrEmpty(item["Protocol"].ToString()))
                {
                    covi.Protocol = item["Protocol"].ToString();
                }
                COVIs.Add(covi);
            }
            var dtcr = from s3 in dtAllDevices.AsEnumerable()
                       where s3.Field<string>("TypeID") == "CCTV_Round"
                       select s3;
            foreach (var item in dtcr)
            {
                CCTV_Round cctv_round = new CCTV_Round();
                cctv_round.Parent = mainRoadPic;
                cctv_round.DeviceID = item["DeviceID"].ToString();
                cctv_round.Name = item["Name"].ToString();
                cctv_round.TypeID = item["TypeID"].ToString();
                cctv_round.DevicePos = item["DevicePos"].ToString();
                cctv_round.PicX = Convert.ToInt32(item["PicX"].ToString());
                cctv_round.PicY = Convert.ToInt32(item["PicY"].ToString());
                cctv_round.Location = new Point(cctv_round.PicX, cctv_round.PicY);
                if (!string.IsNullOrEmpty(item["Protocol"].ToString()))
                {
                    cctv_round.Protocol = item["Protocol"].ToString();
                }
                CCTV_Rounds.Add(cctv_round);
            }
            var dtccc = from s4 in dtAllDevices.AsEnumerable()
                        where s4.Field<string>("TypeID") == "CCTV_CloudColor"
                        select s4;
            foreach (var item in dtccc)
            {
                CCTV_CloudColor cctv_cloudcolor = new CCTV_CloudColor();
                cctv_cloudcolor.Parent = mainRoadPic;
                cctv_cloudcolor.DeviceID = item["DeviceID"].ToString();
                cctv_cloudcolor.Name = item["Name"].ToString();
                cctv_cloudcolor.TypeID = item["TypeID"].ToString();
                cctv_cloudcolor.DevicePos = item["DevicePos"].ToString();
                cctv_cloudcolor.PicX = Convert.ToInt32(item["PicX"].ToString());
                cctv_cloudcolor.PicY = Convert.ToInt32(item["PicY"].ToString());
                cctv_cloudcolor.Location = new Point(cctv_cloudcolor.PicX, cctv_cloudcolor.PicY);
                if (!string.IsNullOrEmpty(item["Protocol"].ToString()))
                {
                    cctv_cloudcolor.Protocol = item["Protocol"].ToString();
                }
                CCTV_CloudColors.Add(cctv_cloudcolor);
            }
            var dtcfc = from s5 in dtAllDevices.AsEnumerable()
                        where s5.Field<string>("TypeID") == "CCTV_FixedColor"
                        select s5;
            foreach (var item in dtcfc)
            {
                CCTV_FixedColor cctv_fixedcolor = new CCTV_FixedColor();
                cctv_fixedcolor.Parent = mainRoadPic;
                cctv_fixedcolor.DeviceID = item["DeviceID"].ToString();
                cctv_fixedcolor.Name = item["Name"].ToString();
                cctv_fixedcolor.TypeID = item["TypeID"].ToString();
                cctv_fixedcolor.DevicePos = item["DevicePos"].ToString();
                cctv_fixedcolor.PicX = Convert.ToInt32(item["PicX"].ToString());
                cctv_fixedcolor.PicY = Convert.ToInt32(item["PicY"].ToString());
                cctv_fixedcolor.Location = new Point(cctv_fixedcolor.PicX, cctv_fixedcolor.PicY);
                if (!string.IsNullOrEmpty(item["Protocol"].ToString()))
                {
                    cctv_fixedcolor.Protocol = item["Protocol"].ToString();
                }
                CCTV_FixedColors.Add(cctv_fixedcolor);
            }

            var dtts = from s6 in dtAllDevices.AsEnumerable()
                       where s6.Field<string>("TypeID") == "TS"
                       select s6;
            foreach (var item in dtts)
            {
                TS ts = new TS();
                ts.Parent = mainRoadPic;
                ts.DeviceID = item["DeviceID"].ToString();
                ts.Name = item["Name"].ToString();
                ts.TypeID = item["TypeID"].ToString();
                ts.DevicePos = item["DevicePos"].ToString();
                ts.PicX = Convert.ToInt32(item["PicX"].ToString());
                ts.PicY = Convert.ToInt32(item["PicY"].ToString());
                ts.Location = new Point(ts.PicX, ts.PicY);
                if (!string.IsNullOrEmpty(item["Protocol"].ToString()))
                {
                    ts.Protocol = item["Protocol"].ToString();
                }
                TSs.Add(ts);
            }
            var dtfd = from s7 in dtAllDevices.AsEnumerable().AsParallel()
                       where s7.Field<string>("TypeID") == "FD"
                       select s7;

            //Parallel.ForEach(dtfd, item =>
            // {
            //     this.Invoke(new Action(() =>
            //     {
            //         FD fd = new FD();
            //         fd.Parent = mainRoadPic;
            //         fd.DeviceID = item["DeviceID"].ToString();
            //         fd.Name = item["Name"].ToString();
            //         fd.TypeID = item["TypeID"].ToString();
            //         fd.DevicePos = item["DevicePos"].ToString();
            //         fd.PicX = Convert.ToInt32(item["PicX"].ToString());
            //         fd.PicY = Convert.ToInt32(item["PicY"].ToString());
            //         fd.Location = new Point(fd.PicX, fd.PicY);
            //         if (!string.IsNullOrEmpty(item["Protocol"].ToString()))
            //         {
            //             fd.Protocol = item["Protocol"].ToString();
            //         }
            //         FDs.Add(fd);
            //     }));
            // });
            foreach (var item in dtfd)
            {
                FD fd = new FD();
                fd.Parent = mainRoadPic;
                fd.DeviceID = item["DeviceID"].ToString();
                fd.Name = item["Name"].ToString();
                fd.TypeID = item["TypeID"].ToString();
                fd.DevicePos = item["DevicePos"].ToString();
                fd.PicX = Convert.ToInt32(item["PicX"].ToString());
                fd.PicY = Convert.ToInt32(item["PicY"].ToString());
                fd.Location = new Point(fd.PicX, fd.PicY);
                if (!string.IsNullOrEmpty(item["Protocol"].ToString()))
                {
                    fd.Protocol = item["Protocol"].ToString();
                }
                FDs.Add(fd);
            }
            var dtfcms = from s8 in dtAllDevices.AsEnumerable()
                         where s8.Field<string>("TypeID") == "FCMS"
                         select s8;
            foreach (var item in dtfcms)
            {
                FCMS fcms = new FCMS();
                fcms.Parent = mainRoadPic;
                fcms.DeviceID = item["DeviceID"].ToString();
                fcms.Name = item["Name"].ToString();
                fcms.TypeID = item["TypeID"].ToString();
                fcms.DevicePos = item["DevicePos"].ToString();
                fcms.PicX = Convert.ToInt32(item["PicX"].ToString());
                fcms.PicY = Convert.ToInt32(item["PicY"].ToString());
                fcms.Location = new Point(fcms.PicX, fcms.PicY);
                if (!string.IsNullOrEmpty(item["Protocol"].ToString()))
                {
                    fcms.Protocol = item["Protocol"].ToString();
                }
                FCMSs.Add(fcms);
            }
            var dtet = from s9 in dtAllDevices.AsEnumerable().AsParallel()
                       where s9.Field<string>("TypeID") == "ET"
                       select s9;

            //Parallel.ForEach(dtet, item =>
            //{
            //    this.Invoke(new Action(() =>
            //    {
            //        ET et = new ET();
            //        et.Parent = mainRoadPic;
            //        et.DeviceID = item["DeviceID"].ToString();
            //        et.Name = item["Name"].ToString();
            //        et.TypeID = item["TypeID"].ToString();
            //        et.DevicePos = item["DevicePos"].ToString();
            //        et.PicX = Convert.ToInt32(item["PicX"].ToString());
            //        et.PicY = Convert.ToInt32(item["PicY"].ToString());
            //        et.Location = new Point(et.PicX, et.PicY);
            //        if (!string.IsNullOrEmpty(item["Protocol"].ToString()))
            //        {
            //            et.Protocol = item["Protocol"].ToString();
            //        }
            //        ETs.Add(et);
            //    }));
            //}
            //);
            foreach (var item in dtet)
            {
                ET et = new ET();
                et.Parent = mainRoadPic;
                et.DeviceID = item["DeviceID"].ToString();
                et.Name = item["Name"].ToString();
                et.TypeID = item["TypeID"].ToString();
                et.DevicePos = item["DevicePos"].ToString();
                et.PicX = Convert.ToInt32(item["PicX"].ToString());
                et.PicY = Convert.ToInt32(item["PicY"].ToString());
                et.Location = new Point(et.PicX, et.PicY);
                if (!string.IsNullOrEmpty(item["Protocol"].ToString()))
                {
                    et.Protocol = item["Protocol"].ToString();
                }
                ETs.Add(et);
            }
            var dtLight_BasicLED10 = from s10 in dtAllDevices.AsEnumerable()
                                     where s10.Field<string>("TypeID") == "Light_BasicLED_50W10"
                                     select s10;
            foreach (var item in dtLight_BasicLED10)
            {
                Light_BasicLED10 lbl10 = new Light_BasicLED10();
                lbl10.Parent = mainRoadPic;
                lbl10.DeviceID = item["DeviceID"].ToString();
                lbl10.Name = item["Name"].ToString();
                lbl10.TypeID = item["TypeID"].ToString();
                lbl10.DevicePos = item["DevicePos"].ToString();
                lbl10.PicX = Convert.ToInt32(item["PicX"].ToString());
                lbl10.PicY = Convert.ToInt32(item["PicY"].ToString());
                lbl10.Location = new Point(lbl10.PicX, lbl10.PicY);
                if (!string.IsNullOrEmpty(item["Protocol"].ToString()))
                {
                    lbl10.Protocol = item["Protocol"].ToString();
                }
                Light_BasicLED10s.Add(lbl10);
            }
            var dtLight_BasicLED21 = from s11 in dtAllDevices.AsEnumerable()
                                     where s11.Field<string>("TypeID") == "Light_BasicLED_50W21"
                                     select s11;
            foreach (var item in dtLight_BasicLED21)
            {
                Light_BasicLED21 lbl21 = new Light_BasicLED21();
                lbl21.Parent = mainRoadPic;
                lbl21.DeviceID = item["DeviceID"].ToString();
                lbl21.Name = item["Name"].ToString();
                lbl21.TypeID = item["TypeID"].ToString();
                lbl21.DevicePos = item["DevicePos"].ToString();
                lbl21.PicX = Convert.ToInt32(item["PicX"].ToString());
                lbl21.PicY = Convert.ToInt32(item["PicY"].ToString());
                lbl21.Location = new Point(lbl21.PicX, lbl21.PicY);
                if (!string.IsNullOrEmpty(item["Protocol"].ToString()))
                {
                    lbl21.Protocol = item["Protocol"].ToString();
                }
                Light_BasicLED21s.Add(lbl21);
            }
            var dtLight_BasicLED22 = from s12 in dtAllDevices.AsEnumerable()
                                     where s12.Field<string>("TypeID") == "Light_BasicLED_50W22"
                                     select s12;
            foreach (var item in dtLight_BasicLED22)
            {
                Light_BasicLED22 lbl22 = new Light_BasicLED22();
                lbl22.Parent = mainRoadPic;
                lbl22.DeviceID = item["DeviceID"].ToString();
                lbl22.Name = item["Name"].ToString();
                lbl22.TypeID = item["TypeID"].ToString();
                lbl22.DevicePos = item["DevicePos"].ToString();
                lbl22.PicX = Convert.ToInt32(item["PicX"].ToString());
                lbl22.PicY = Convert.ToInt32(item["PicY"].ToString());
                lbl22.Location = new Point(lbl22.PicX, lbl22.PicY);
                if (!string.IsNullOrEmpty(item["Protocol"].ToString()))
                {
                    lbl22.Protocol = item["Protocol"].ToString();
                }
                Light_BasicLED22s.Add(lbl22);
            }
            var dtLight_CloudS1 = from s13 in dtAllDevices.AsEnumerable()
                                  where s13.Field<string>("TypeID") == "Light_CloudS_150W1"
                                  select s13;
            foreach (var item in dtLight_CloudS1)
            {
                Light_CloudS1 lc1 = new Light_CloudS1();
                lc1.Parent = mainRoadPic;
                lc1.DeviceID = item["DeviceID"].ToString();
                lc1.Name = item["Name"].ToString();
                lc1.TypeID = item["TypeID"].ToString();
                lc1.DevicePos = item["DevicePos"].ToString();
                lc1.PicX = Convert.ToInt32(item["PicX"].ToString());
                lc1.PicY = Convert.ToInt32(item["PicY"].ToString());
                lc1.Location = new Point(lc1.PicX, lc1.PicY);
                if (!string.IsNullOrEmpty(item["Protocol"].ToString()))
                {
                    lc1.Protocol = item["Protocol"].ToString();
                }
                Light_CloudS1s.Add(lc1);
            }
            var dtLight_CloudS2 = from s14 in dtAllDevices.AsEnumerable()
                                  where s14.Field<string>("TypeID") == "Light_CloudS_150W2"
                                  select s14;
            foreach (var item in dtLight_CloudS2)
            {
                Light_CloudS2 lc2 = new Light_CloudS2();
                lc2.Parent = mainRoadPic;
                lc2.DeviceID = item["DeviceID"].ToString();
                lc2.Name = item["Name"].ToString();
                lc2.TypeID = item["TypeID"].ToString();
                lc2.DevicePos = item["DevicePos"].ToString();
                lc2.PicX = Convert.ToInt32(item["PicX"].ToString());
                lc2.PicY = Convert.ToInt32(item["PicY"].ToString());
                lc2.Location = new Point(lc2.PicX, lc2.PicY);
                if (!string.IsNullOrEmpty(item["Protocol"].ToString()))
                {
                    lc2.Protocol = item["Protocol"].ToString();
                }
                Light_CloudS2s.Add(lc2);
            }
            var dtLight_EmergencyLED = from s15 in dtAllDevices.AsEnumerable()
                                       where s15.Field<string>("TypeID") == "Light_EmergencyLED_50W"
                                       select s15;
            foreach (var item in dtLight_EmergencyLED)
            {
                Light_EmergencyLED lel = new Light_EmergencyLED();
                lel.Parent = mainRoadPic;
                lel.DeviceID = item["DeviceID"].ToString();
                lel.Name = item["Name"].ToString();
                lel.TypeID = item["TypeID"].ToString();
                lel.DevicePos = item["DevicePos"].ToString();
                lel.PicX = Convert.ToInt32(item["PicX"].ToString());
                lel.PicY = Convert.ToInt32(item["PicY"].ToString());
                lel.Location = new Point(lel.PicX, lel.PicY);
                if (!string.IsNullOrEmpty(item["Protocol"].ToString()))
                {
                    lel.Protocol = item["Protocol"].ToString();
                }
                Light_EmergencyLEDs.Add(lel);
            }
            var dtLight_SunnyS1 = from s16 in dtAllDevices.AsEnumerable()
                                  where s16.Field<string>("TypeID") == "Light_SunnyS_150W1"
                                  select s16;
            foreach (var item in dtLight_SunnyS1)
            {
                Light_SunnyS1 ls1 = new Light_SunnyS1();
                ls1.Parent = mainRoadPic;
                ls1.DeviceID = item["DeviceID"].ToString();
                ls1.Name = item["Name"].ToString();
                ls1.TypeID = item["TypeID"].ToString();
                ls1.DevicePos = item["DevicePos"].ToString();
                ls1.PicX = Convert.ToInt32(item["PicX"].ToString());
                ls1.PicY = Convert.ToInt32(item["PicY"].ToString());
                ls1.Location = new Point(ls1.PicX, ls1.PicY);
                if (!string.IsNullOrEmpty(item["Protocol"].ToString()))
                {
                    ls1.Protocol = item["Protocol"].ToString();
                }
                Light_SunnyS1s.Add(ls1);
            }
            var dtLight_SunnyS2 = from s16 in dtAllDevices.AsEnumerable()
                                  where s16.Field<string>("TypeID") == "Light_SunnyS_150W2"
                                  select s16;
            foreach (var item in dtLight_SunnyS2)
            {
                Light_SunnyS2 ls2 = new Light_SunnyS2();
                ls2.Parent = mainRoadPic;
                ls2.DeviceID = item["DeviceID"].ToString();
                ls2.Name = item["Name"].ToString();
                ls2.TypeID = item["TypeID"].ToString();
                ls2.DevicePos = item["DevicePos"].ToString();
                ls2.PicX = Convert.ToInt32(item["PicX"].ToString());
                ls2.PicY = Convert.ToInt32(item["PicY"].ToString());
                ls2.Location = new Point(ls2.PicX, ls2.PicY);
                if (!string.IsNullOrEmpty(item["Protocol"].ToString()))
                {
                    ls2.Protocol = item["Protocol"].ToString();
                }
                Light_SunnyS2s.Add(ls2);
            }
            var dtLight_Out = from s17 in dtAllDevices.AsEnumerable()
                              where s17.Field<string>("TypeID") == "Light_Out_90W"
                              select s17;
            foreach (var item in dtLight_Out)
            {
                Light_Out lo = new Light_Out();
                lo.Parent = mainRoadPic;
                lo.DeviceID = item["DeviceID"].ToString();
                lo.Name = item["Name"].ToString();
                lo.TypeID = item["TypeID"].ToString();
                lo.DevicePos = item["DevicePos"].ToString();
                lo.PicX = Convert.ToInt32(item["PicX"].ToString());
                lo.PicY = Convert.ToInt32(item["PicY"].ToString());
                lo.Location = new Point(lo.PicX, lo.PicY);
                if (!string.IsNullOrEmpty(item["Protocol"].ToString()))
                {
                    lo.Protocol = item["Protocol"].ToString();
                }
                Light_Outs.Add(lo);
            }
            var dtFiber = from s18 in dtAllDevices.AsEnumerable()
                          where s18.Field<string>("TypeID") == "Fiber"
                          select s18;

            foreach (var item in dtFiber)
            {
                FiberPoint fiber = new FiberPoint();
                fiber.Parent = mainRoadPic;
                fiber.DeviceID = item["DeviceID"].ToString();
                fiber.Name = item["Name"].ToString();
                fiber.TypeID = item["TypeID"].ToString();
                fiber.DevicePos = item["DevicePos"].ToString();
                fiber.PicX = Convert.ToInt32(item["PicX"].ToString());
                fiber.PicY = Convert.ToInt32(item["PicY"].ToString());
                fiber.Location = new Point(Convert.ToInt32(item["PicX"].ToString()), Convert.ToInt32(item["PicY"].ToString()));
                if (!string.IsNullOrEmpty(item["Protocol"].ToString()))
                {
                    fiber.Protocol = item["Protocol"].ToString();
                }
                Fibers.Add(fiber);
            }
            var dtSomke = from s19 in dtAllDevices.AsEnumerable()
                          where s19.Field<string>("TypeID") == "Somke"
                          select s19;

            foreach (var item in dtSomke)
            {
                SOMKE somke = new SOMKE();
                somke.Parent = mainRoadPic;
                somke.DeviceID = item["DeviceID"].ToString();
                somke.Name = item["Name"].ToString();
                somke.TypeID = item["TypeID"].ToString();
                somke.DevicePos = item["DevicePos"].ToString();
                somke.PicX = Convert.ToInt32(item["PicX"].ToString());
                somke.PicY = Convert.ToInt32(item["PicY"].ToString());
                somke.Location = new Point(Convert.ToInt32(item["PicX"].ToString()), Convert.ToInt32(item["PicY"].ToString()));
                if (!string.IsNullOrEmpty(item["Protocol"].ToString()))
                {
                    somke.Protocol = item["Protocol"].ToString();
                }
                Somkes.Add(somke);
            }
            var dtLCs = from s1 in dtAllDevices.AsEnumerable()
                        where s1.Field<string>("TypeID") == "LC"
                        select s1;

            foreach (var item in dtLCs)
            {
                LC lc = new LC();
                lc.Parent = mainRoadPic;
                lc.DeviceID = item["DeviceID"].ToString();
                lc.Name = item["Name"].ToString();
                lc.TypeID = item["TypeID"].ToString();
                lc.DevicePos = item["DevicePos"].ToString();
                lc.PicX = Convert.ToInt32(item["PicX"].ToString());
                lc.PicY = Convert.ToInt32(item["PicY"].ToString());
                lc.Location = new Point(lc.PicX, lc.PicY);
                LCs.Add(lc);
                Global.PlcStatus.Add(lc.DeviceID, true);

            }
            #endregion
        }

        #region 拖动路面代码片段，该版本为不允许拖动路面版本
        Point mouseDownPoint = new Point(); //记录拖拽过程鼠标位置
        bool isMove = false;    //判断鼠标在picturebox上移动时，是否处于拖拽过程(鼠标左键是否按下)
                                //int zoomStep = 20;      //缩放步长

        private void MainForm_Resize(object sender, EventArgs e)
        {
            mainPanel.Location = new Point(0, Convert.ToInt32(this.Height * 0.15));
            mainPanel.Size = new Size(this.Width, Convert.ToInt32(this.Height * 0.85));
            mainRoadPic.Location = new Point(0, 0);
            //mainRoadPic.Size = new Size(1920,765);
            //mainRoadPic.Size = new Size(1920, 918);
            mainRoadPic.Size = mainPanel.Size;
        }

        private void mainRoadPic_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseDownPoint.X = Cursor.Position.X;
                mouseDownPoint.Y = Cursor.Position.Y;
                isMove = true;
                mainRoadPic.Focus();
            }
        }

        private void mainRoadPic_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isMove = false;
            }
            if (mainRoadPic.Location.X > 0 && mainRoadPic.Location.Y > 0)
            {
                mainRoadPic.Location = new Point(0, 0);
            }
            if (mainRoadPic.Location.X > 0)
            {
                mainRoadPic.Location = new Point(0, y);
            }
            if (mainRoadPic.Location.Y > 0)
            {
                mainRoadPic.Location = new Point(x, 0);
            }
            if (mainRoadPic.Location.X < -(mainRoadPic.Size.Width - mainPanel.Width) && mainRoadPic.Location.Y < -(mainRoadPic.Size.Height - mainPanel.Height))
            {
                mainRoadPic.Location = new Point(-(mainRoadPic.Size.Width - mainPanel.Width), -(mainRoadPic.Size.Height - mainPanel.Height));
            }
            if (mainRoadPic.Location.X < -(mainRoadPic.Size.Width - mainPanel.Width))
            {
                mainRoadPic.Location = new Point(-(mainRoadPic.Size.Width - mainPanel.Width), y);
            }
            if (mainRoadPic.Location.Y < -(mainRoadPic.Size.Height - mainPanel.Height))
            {
                mainRoadPic.Location = new Point(x, -(mainRoadPic.Size.Height - mainPanel.Height));
            }

            //label1.Text = mainRoadPic.Location.ToString();

        }
        int x, y;
        private void mainRoadPic_MouseMove(object sender, MouseEventArgs e)
        {
            mainRoadPic.Focus();
            if (isMove)
            {

                int moveX, moveY;
                //moveX = Cursor.Position.X - mouseDownPoint.X;
                //moveY = Cursor.Position.Y - mouseDownPoint.Y;
                moveX = 0; //不允许Y轴拖动
                moveY = 0; //只允许X轴拖动
                x = mainRoadPic.Location.X + moveX;
                y = mainRoadPic.Location.Y + moveY;
                mainRoadPic.Location = new Point(x, y);

                mouseDownPoint.X = Cursor.Position.X;
                mouseDownPoint.Y = Cursor.Position.Y;

                //label1.Text = mainRoadPic.Height.ToString() + "  " + mainRoadPic.Width.ToString()+"            "+mainPanel.Width.ToString()+"   "+mainPanel.Height.ToString();
            }
        }

        private void mainPanel_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseDownPoint.X = Cursor.Position.X;
                mouseDownPoint.Y = Cursor.Position.Y;
                isMove = true;
            }
        }

        private void mainPanel_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isMove = false;
            }
            if (mainRoadPic.Location.X > 0)
            {
                mainRoadPic.Location = new Point(0, y);
            }
            if (mainRoadPic.Location.Y > 0)
            {
                mainRoadPic.Location = new Point(x, 0);
            }
            if (mainRoadPic.Location.X < -(mainRoadPic.Size.Width - mainPanel.Width))
            {
                mainRoadPic.Location = new Point(-(mainRoadPic.Size.Width - mainPanel.Width), y);
            }
            if (mainRoadPic.Location.Y < -(mainRoadPic.Size.Height - mainPanel.Height))
            {
                mainRoadPic.Location = new Point(x, -(mainRoadPic.Size.Height - mainPanel.Height));
            }


        }
        #endregion


        private void menuEntry_MouseEnter(object sender, EventArgs e)
        {
            mainMenu.Visible = true;

            menuEntry.Visible = false;
            mainMenu.Location = new Point(0, Convert.ToInt32(this.Height * 0.15));
        }

        private void mainMenu_MouseLeave(object sender, EventArgs e)
        {
            mainMenu.Visible = false;
            menuEntry.Visible = true;
        }

        private void AppExit()
        {
            DialogResult dr = MessageBox.Show("您确定要退出本监控程序么？", "退出确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
            if (dr == DialogResult.Yes)
            {
                Tools.FanTimeControl.SaveLastTime();
                dcsHost.Close();
                Application.Exit();
            }
        }

        private void mainRoadPic_MouseClick(object sender, MouseEventArgs e)
        {
            mainMenu.Visible = false;
            menuEntry.Visible = true;
        }

        private void refreshBackground_DoWork(object sender, DoWorkEventArgs e)
        {
            string sqlAllStatus = "SELECT M_Device.DeviceID, M_DevicesStatus.StatusID, M_DevicesStatus.StatusDescription, M_Device.UpdateTime FROM M_Device LEFT OUTER JOIN  M_DevicesStatus ON M_Device.DeviceStatusID = M_DevicesStatus.StatusID";
            SqlDBHelper sdh = new SqlDBHelper();
            DataTable allStatusDt = sdh.GetTable(sqlAllStatus);

            List<decimal> list = new List<decimal>();
            string sql = "select * from R_RunStatus where RSID='COVI1-co'";
            string sql2 = "select * from R_RunStatus where RSID='COVI1-vi'";
            //SqlDBHelper sdh = new SqlDBHelper();
            DataTable dt = sdh.GetTable(sql);
            DataTable dt2 = sdh.GetTable(sql2);

            list.Add(Convert.ToDecimal(dt.Rows[0][6].ToString()));
            list.Add(Convert.ToDecimal(dt2.Rows[0][6].ToString()));
            //向前台报告COVI值
            refreshBackground.ReportProgress(50, list);
            //返回全部设备状态
            e.Result = allStatusDt;
        }

        private void refreshBackground_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            allStatusTable = e.Result as DataTable;
            refreshDeviceStatus();
        }
        /// <summary>
        /// 将allStatusTable内的设备状态更新至界面
        /// </summary>
        private void refreshDeviceStatus()
        {
            foreach (Fan d in FANs)
            {
                DataRow dr = allStatusTable.Select("DeviceID='" + d.DeviceID + "'")[0];
                if (dr["StatusID"].ToString() != string.Empty)
                {
                    d.StatusID = dr["StatusID"].ToString();
                }
                if (dr["StatusDescription"].ToString() != string.Empty)
                {
                    d.StatusDescription = dr["StatusDescription"].ToString();
                }
                if (dr["UpdateTime"].ToString() != string.Empty)
                {
                    d.UpdateTime = Convert.ToDateTime(dr["UpdateTime"].ToString());
                }
            }
            foreach (COVI d in COVIs)
            {
                DataRow dr = allStatusTable.Select("DeviceID='" + d.DeviceID + "'")[0];
                if (dr["StatusID"].ToString() != string.Empty)
                {
                    d.StatusID = dr["StatusID"].ToString();
                }
                if (dr["StatusDescription"].ToString() != string.Empty)
                {
                    d.StatusDescription = dr["StatusDescription"].ToString();
                }
                if (dr["UpdateTime"].ToString() != string.Empty)
                {
                    d.UpdateTime = Convert.ToDateTime(dr["UpdateTime"].ToString());
                }
            }
            foreach (CCTV_Round d in CCTV_Rounds)
            {
                DataRow dr = allStatusTable.Select("DeviceID='" + d.DeviceID + "'")[0];
                if (dr["StatusID"].ToString() != string.Empty)
                {
                    d.StatusID = dr["StatusID"].ToString();
                }
                if (dr["StatusDescription"].ToString() != string.Empty)
                {
                    d.StatusDescription = dr["StatusDescription"].ToString();
                }
                if (dr["UpdateTime"].ToString() != string.Empty)
                {
                    d.UpdateTime = Convert.ToDateTime(dr["UpdateTime"].ToString());
                }
            }
            foreach (CCTV_CloudColor d in CCTV_CloudColors)
            {
                DataRow dr = allStatusTable.Select("DeviceID='" + d.DeviceID + "'")[0];
                if (dr["StatusID"].ToString() != string.Empty)
                {
                    d.StatusID = dr["StatusID"].ToString();
                }
                if (dr["StatusDescription"].ToString() != string.Empty)
                {
                    d.StatusDescription = dr["StatusDescription"].ToString();
                }
                if (dr["UpdateTime"].ToString() != string.Empty)
                {
                    d.UpdateTime = Convert.ToDateTime(dr["UpdateTime"].ToString());
                }
            }
            foreach (CCTV_FixedColor d in CCTV_FixedColors)
            {
                DataRow dr = allStatusTable.Select("DeviceID='" + d.DeviceID + "'")[0];
                if (dr["StatusID"].ToString() != string.Empty)
                {
                    d.StatusID = dr["StatusID"].ToString();
                }
                if (dr["StatusDescription"].ToString() != string.Empty)
                {
                    d.StatusDescription = dr["StatusDescription"].ToString();
                }
                if (dr["UpdateTime"].ToString() != string.Empty)
                {
                    d.UpdateTime = Convert.ToDateTime(dr["UpdateTime"].ToString());
                }
            }
            foreach (TS d in TSs)
            {
                DataRow dr = allStatusTable.Select("DeviceID='" + d.DeviceID + "'")[0];
                if (dr["StatusID"].ToString() != string.Empty)
                {
                    d.StatusID = dr["StatusID"].ToString();
                }
                if (dr["StatusDescription"].ToString() != string.Empty)
                {
                    d.StatusDescription = dr["StatusDescription"].ToString();
                }
                if (dr["UpdateTime"].ToString() != string.Empty)
                {
                    d.UpdateTime = Convert.ToDateTime(dr["UpdateTime"].ToString());
                }
            }
            foreach (FD d in FDs)
            {
                DataRow dr = allStatusTable.Select("DeviceID='" + d.DeviceID + "'")[0];
                if (dr["StatusID"].ToString() != string.Empty)
                {
                    d.StatusID = dr["StatusID"].ToString();
                }
                if (dr["StatusDescription"].ToString() != string.Empty)
                {
                    d.StatusDescription = dr["StatusDescription"].ToString();
                }
                if (dr["UpdateTime"].ToString() != string.Empty)
                {
                    d.UpdateTime = Convert.ToDateTime(dr["UpdateTime"].ToString());
                }
            }
            foreach (FCMS d in FCMSs)
            {
                DataRow dr = allStatusTable.Select("DeviceID='" + d.DeviceID + "'")[0];
                if (dr["StatusID"].ToString() != string.Empty)
                {
                    d.StatusID = dr["StatusID"].ToString();
                }
                if (dr["StatusDescription"].ToString() != string.Empty)
                {
                    d.StatusDescription = dr["StatusDescription"].ToString();
                }
                if (dr["UpdateTime"].ToString() != string.Empty)
                {
                    d.UpdateTime = Convert.ToDateTime(dr["UpdateTime"].ToString());
                }
            }
            foreach (ET d in ETs)
            {
                DataRow dr = allStatusTable.Select("DeviceID='" + d.DeviceID + "'")[0];
                if (dr["StatusID"].ToString() != string.Empty)
                {
                    d.StatusID = dr["StatusID"].ToString();
                }
                if (dr["StatusDescription"].ToString() != string.Empty)
                {
                    d.StatusDescription = dr["StatusDescription"].ToString();
                }
                if (dr["UpdateTime"].ToString() != string.Empty)
                {
                    d.UpdateTime = Convert.ToDateTime(dr["UpdateTime"].ToString());
                }
            }
            foreach (Light_BasicLED10 d in Light_BasicLED10s)
            {
                DataRow dr = allStatusTable.Select("DeviceID='" + d.DeviceID + "'")[0];
                if (dr["StatusID"].ToString() != string.Empty)
                {
                    d.StatusID = dr["StatusID"].ToString();
                }
                if (dr["StatusDescription"].ToString() != string.Empty)
                {
                    d.StatusDescription = dr["StatusDescription"].ToString();
                }
                if (dr["UpdateTime"].ToString() != string.Empty)
                {
                    d.UpdateTime = Convert.ToDateTime(dr["UpdateTime"].ToString());
                }
            }
            foreach (Light_BasicLED21 d in Light_BasicLED21s)
            {
                DataRow dr = allStatusTable.Select("DeviceID='" + d.DeviceID + "'")[0];
                if (dr["StatusID"].ToString() != string.Empty)
                {
                    d.StatusID = dr["StatusID"].ToString();
                }
                if (dr["StatusDescription"].ToString() != string.Empty)
                {
                    d.StatusDescription = dr["StatusDescription"].ToString();
                }
                if (dr["UpdateTime"].ToString() != string.Empty)
                {
                    d.UpdateTime = Convert.ToDateTime(dr["UpdateTime"].ToString());
                }
            }
            foreach (Light_BasicLED22 d in Light_BasicLED22s)
            {
                DataRow dr = allStatusTable.Select("DeviceID='" + d.DeviceID + "'")[0];
                if (dr["StatusID"].ToString() != string.Empty)
                {
                    d.StatusID = dr["StatusID"].ToString();
                }
                if (dr["StatusDescription"].ToString() != string.Empty)
                {
                    d.StatusDescription = dr["StatusDescription"].ToString();
                }
                if (dr["UpdateTime"].ToString() != string.Empty)
                {
                    d.UpdateTime = Convert.ToDateTime(dr["UpdateTime"].ToString());
                }
            }
            foreach (Light_CloudS1 d in Light_CloudS1s)
            {
                DataRow dr = allStatusTable.Select("DeviceID='" + d.DeviceID + "'")[0];
                if (dr["StatusID"].ToString() != string.Empty)
                {
                    d.StatusID = dr["StatusID"].ToString();
                }
                if (dr["StatusDescription"].ToString() != string.Empty)
                {
                    d.StatusDescription = dr["StatusDescription"].ToString();
                }
                if (dr["UpdateTime"].ToString() != string.Empty)
                {
                    d.UpdateTime = Convert.ToDateTime(dr["UpdateTime"].ToString());
                }
            }
            foreach (Light_CloudS2 d in Light_CloudS2s)
            {
                DataRow dr = allStatusTable.Select("DeviceID='" + d.DeviceID + "'")[0];
                if (dr["StatusID"].ToString() != string.Empty)
                {
                    d.StatusID = dr["StatusID"].ToString();
                }
                if (dr["StatusDescription"].ToString() != string.Empty)
                {
                    d.StatusDescription = dr["StatusDescription"].ToString();
                }
                if (dr["UpdateTime"].ToString() != string.Empty)
                {
                    d.UpdateTime = Convert.ToDateTime(dr["UpdateTime"].ToString());
                }
            }
            foreach (Light_EmergencyLED d in Light_EmergencyLEDs)
            {
                DataRow dr = allStatusTable.Select("DeviceID='" + d.DeviceID + "'")[0];
                if (dr["StatusID"].ToString() != string.Empty)
                {
                    d.StatusID = dr["StatusID"].ToString();
                }
                if (dr["StatusDescription"].ToString() != string.Empty)
                {
                    d.StatusDescription = dr["StatusDescription"].ToString();
                }
                if (dr["UpdateTime"].ToString() != string.Empty)
                {
                    d.UpdateTime = Convert.ToDateTime(dr["UpdateTime"].ToString());
                }
            }
            foreach (Light_Out d in Light_Outs)
            {
                DataRow dr = allStatusTable.Select("DeviceID='" + d.DeviceID + "'")[0];
                if (dr["StatusID"].ToString() != string.Empty)
                {
                    d.StatusID = dr["StatusID"].ToString();
                }
                if (dr["StatusDescription"].ToString() != string.Empty)
                {
                    d.StatusDescription = dr["StatusDescription"].ToString();
                }
                if (dr["UpdateTime"].ToString() != string.Empty)
                {
                    d.UpdateTime = Convert.ToDateTime(dr["UpdateTime"].ToString());
                }
            }

            foreach (Light_SunnyS1 d in Light_SunnyS1s)
            {
                DataRow dr = allStatusTable.Select("DeviceID='" + d.DeviceID + "'")[0];
                if (dr["StatusID"].ToString() != string.Empty)
                {
                    d.StatusID = dr["StatusID"].ToString();
                }
                if (dr["StatusDescription"].ToString() != string.Empty)
                {
                    d.StatusDescription = dr["StatusDescription"].ToString();
                }
                if (dr["UpdateTime"].ToString() != string.Empty)
                {
                    d.UpdateTime = Convert.ToDateTime(dr["UpdateTime"].ToString());
                }
            }
            foreach (Light_SunnyS2 d in Light_SunnyS2s)
            {
                DataRow dr = allStatusTable.Select("DeviceID='" + d.DeviceID + "'")[0];
                if (dr["StatusID"].ToString() != string.Empty)
                {
                    d.StatusID = dr["StatusID"].ToString();
                }
                if (dr["StatusDescription"].ToString() != string.Empty)
                {
                    d.StatusDescription = dr["StatusDescription"].ToString();
                }
                if (dr["UpdateTime"].ToString() != string.Empty)
                {
                    d.UpdateTime = Convert.ToDateTime(dr["UpdateTime"].ToString());
                }
            }
            foreach (FiberPoint d in Fibers)
            {
                DataRow dr = allStatusTable.Select("DeviceID='" + d.DeviceID + "'")[0];
                if (dr["StatusID"].ToString() != string.Empty)
                {
                    d.StatusID = dr["StatusID"].ToString();
                }
                if (dr["StatusDescription"].ToString() != string.Empty)
                {
                    d.StatusDescription = dr["StatusDescription"].ToString();
                }
                if (dr["UpdateTime"].ToString() != string.Empty)
                {
                    d.UpdateTime = Convert.ToDateTime(dr["UpdateTime"].ToString());
                }
            }
            foreach (SOMKE d in Somkes)
            {
                DataRow dr = allStatusTable.Select("DeviceID='" + d.DeviceID + "'")[0];
                if (dr["StatusID"].ToString() != string.Empty)
                {
                    d.StatusID = dr["StatusID"].ToString();
                }
                if (dr["StatusDescription"].ToString() != string.Empty)
                {
                    d.StatusDescription = dr["StatusDescription"].ToString();
                }
                if (dr["UpdateTime"].ToString() != string.Empty)
                {
                    d.UpdateTime = Convert.ToDateTime(dr["UpdateTime"].ToString());
                }
            }
            foreach (LC d in LCs)
            {
                DataRow dr = allStatusTable.Select("DeviceID='" + d.DeviceID + "'")[0];
                if (dr["StatusID"].ToString() != string.Empty)
                {
                    d.StatusID = dr["StatusID"].ToString();
                    if (dr["StatusID"].ToString() == "060")
                    {
                        Global.PlcStatus[d.DeviceID] = true;
                    }
                    if (dr["StatusID"].ToString() == "061")
                    {
                        Global.PlcStatus[d.DeviceID] = false;
                    }
                }
                if (dr["StatusDescription"].ToString() != string.Empty)
                {
                    d.StatusDescription = dr["StatusDescription"].ToString();
                }
                if (dr["UpdateTime"].ToString() != string.Empty)
                {
                    d.UpdateTime = Convert.ToDateTime(dr["UpdateTime"].ToString());
                }
            }
        }

        private void refreshBackground_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            if (e.ProgressPercentage == 50)
            {
                List<decimal> list = e.UserState as List<decimal>;
                COVIs[0].Co = list[0];
                COVIs[0].Vi = list[1];
            }
        }

        private void mainTimer_Tick(object sender, EventArgs e)
        {
            if (!refreshBackground.IsBusy)
            {
                refreshBackground.RunWorkerAsync();
            }
        }

        private void clearDeviceInRoad()
        {
            foreach (Control c in mainRoadPic.Controls)
            {
                c.Visible = false;
            }
        }
        /// <summary>
        /// 显示菜单1所包含设备
        /// </summary>
        private void ShowDeviceGroup1()
        {
            clearDeviceInRoad();
            foreach (Fan fan in FANs)
            {
                fan.Visible = true;
            }
            foreach (Light_BasicLED10 lbl10 in Light_BasicLED10s)
            {
                lbl10.Visible = true;
            }
            foreach (Light_BasicLED21 lbl21 in Light_BasicLED21s)
            {
                lbl21.Visible = true;
            }
            foreach (Light_BasicLED22 lbl22 in Light_BasicLED22s)
            {
                lbl22.Visible = true;
            }
            foreach (Light_CloudS1 lc1 in Light_CloudS1s)
            {
                lc1.Visible = true;
            }
            foreach (Light_CloudS2 lc2 in Light_CloudS2s)
            {
                lc2.Visible = true;
            }
            foreach (Light_EmergencyLED lel in Light_EmergencyLEDs)
            {
                lel.Visible = true;
            }
            foreach (Light_SunnyS1 ls1 in Light_SunnyS1s)
            {
                ls1.Visible = true;
            }
            foreach (Light_SunnyS2 ls2 in Light_SunnyS2s)
            {
                ls2.Visible = true;
            }
            foreach (Light_Out lo in Light_Outs)
            {
                lo.Visible = true;
            }
            foreach (COVI covi in COVIs)
            {
                covi.Visible = true;
            }

            foreach (TS ts in TSs)
            {
                ts.Visible = true;
            }

            foreach (FD fd in FDs)
            {
                fd.Visible = true;
            }
            foreach (FiberPoint fiber in Fibers)
            {
                fiber.Visible = true;
            }
            foreach (FCMS fcms in FCMSs)
            {
                fcms.Visible = true;
            }
            foreach (SOMKE somke in Somkes)
            {
                somke.Visible = true;
            }
            foreach (LC lc in LCs)
            {
                lc.Visible = true;
            }
        }
        /// <summary>
        /// 显示菜单2包含设备
        /// </summary>
        private void ShowDeviceGroup2()
        {
            clearDeviceInRoad();
            foreach (FD fd in FDs)
            {
                fd.Visible = true;
            }
            //foreach (ET et in ETs)
            //{
            //    et.Visible = true;
            //}
            foreach (FiberPoint fiber in Fibers)
            {
                fiber.Visible = true;
            }
        }
        /// <summary>
        /// 显示菜单3包含设备
        /// </summary>
        private void ShowDeviceGroup3()
        {
            clearDeviceInRoad();
            //foreach (CCTV_Round cr in CCTV_Rounds)
            //{
            //    cr.Visible = true;
            //}
            //foreach (CCTV_CloudColor ccc in CCTV_CloudColors)
            //{
            //    ccc.Visible = true;
            //}
            //foreach (CCTV_FixedColor cfc in CCTV_FixedColors)
            //{
            //    cfc.Visible = true;
            //}
            foreach (FCMS fcms in FCMSs)
            {
                fcms.Visible = true;
            }
        }

        private void menuPic1_Click(object sender, EventArgs e)
        {
            //ShowDeviceGroup1();

        }

        private void menuPic2_Click(object sender, EventArgs e)
        {
            //ShowDeviceGroup2();
            AlarmForm f = new AlarmForm();
            f.StartPosition = FormStartPosition.CenterScreen;
            f.ShowDialog();
        }

        private void menuPic3_Click(object sender, EventArgs e)
        {
            //ShowDeviceGroup3();
            ManualEventForm f = new ManualEventForm();
            f.StartPosition = FormStartPosition.CenterScreen;
            f.ShowDialog();
        }
        private void menuPic4_Click(object sender, EventArgs e)
        {
            PlanForm f = new PlanForm();
            f.StartPosition = FormStartPosition.CenterScreen;
            f.ShowDialog();
        }

        private void appExitPic_Click(object sender, EventArgs e)
        {
            AppExit();
        }

        private void mainPanel_MouseMove(object sender, MouseEventArgs e)
        {
            //mainPanel.Focus();
            //if (isMove)
            //{
            //    int x, y;
            //    int moveX, moveY;
            //    moveX = Cursor.Position.X - mouseDownPoint.X;
            //    moveY = Cursor.Position.Y - mouseDownPoint.Y;
            //    x = mainRoadPic.Location.X + moveX;
            //    y = mainRoadPic.Location.Y + moveY;
            //    mainRoadPic.Location = new Point(x, y);
            //    if (mainRoadPic.Location.X > 0)
            //    {
            //        mainRoadPic.Location = new Point(0, y);
            //    }
            //    if (mainRoadPic.Location.Y > 0)
            //    {
            //        mainRoadPic.Location = new Point(x, 0);
            //    }
            //    if (mainRoadPic.Location.X < -(mainRoadPic.Size.Width - mainPanel.Width))
            //    {
            //        mainRoadPic.Location = new Point(-(mainRoadPic.Size.Width - mainPanel.Width), y);
            //    }
            //    if (mainRoadPic.Location.Y < -(mainRoadPic.Size.Height - mainPanel.Height))
            //    {
            //        mainRoadPic.Location = new Point(x, -(mainRoadPic.Size.Height - mainPanel.Height));
            //    }
            //    mouseDownPoint.X = Cursor.Position.X;
            //    mouseDownPoint.Y = Cursor.Position.Y;
            //}
        }

        //private void refreshStatus()
        //{
        //    foreach(Control c in this.mainRoadPic.Controls)
        //    {
        //        if(c is Fan)
        //        textBox1.Text = textBox1.Text+((Fan)c).Name + "   "+((Fan)c).Name + "\r\n";
        //    }
        //}
    }
}
